<template>
  <div class="com-container">
    <div class="com-chart" ref="bili_ref"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      chartInstance: null
    }
  },
  destroyed () {
    window.removeEventListener('resize', this.updataChart)
  },
  mounted () {
    this.initChart()
    this.updataChart()
    window.addEventListener('resize', this.updataChart)
    this.updataChart()
  },
  methods: {
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.bili_ref)
    },
    updataChart () {
      const option = {
        backgroundColor: '#F5F5F5',
        title: {
          text: '非遗项目入选年份分布',
          subtext: '数据来源：中国非物质文化遗产网',
          x: 'center',
          y: '3%',
          textStyle: {
            fontFamily: 'sans-serif',
            fontSize: 22,
            fontWeight: 'normal'
          },
          subtextStyle: {
            color: '#646464',
            fontFamily: 'sans-serif',
            fontSize: 16,
            fontWeight: 'normal'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['人类非物质文化遗产代表作名录', '急需保护的非物质文化遗产名录', '优秀实践名册'],
          top: '10%'
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          top: '25%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: ['2008', '2009', '2010', '2011', '2012', '2013', '2016', '2018', '2020', '2022', '2024'],
          axisLabel: {
            rotate: 45
          }
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '人类非物质文化遗产代表作名录',
            type: 'bar',
            stack: 'total',
            data: [2, 22, 1, 1, 0, 1, 1, 1, 2, 1, 4]
          },
          {
            name: '急需保护的非物质文化遗产名录',
            type: 'bar',
            stack: 'total',
            data: [0, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
          },
          {
            name: '优秀实践名册',
            type: 'bar',
            stack: 'total',
            data: [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
          }
        ]
      }
      this.chartInstance.setOption(option)
      this.chartInstance.resize()
    }
  }
}
</script>

<style>
</style>
